package com.sansui.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sansui.blog.dao.CommentDao;
import com.sansui.blog.pojo.Comment;
import com.sansui.blog.service.CommentService;
import com.sansui.blog.utils.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @name: CommentServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-04-03 00:51
 **/
@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentDao commentDao;

//    默认头像地址
    @Value("${comment.avatar}")
    private String avatar;

//    获取ip接口地址
    @Value("${comment.ipApi-url}")
    private String ipApiUrl;

    @Override
    public Integer countcomments() {
        return commentDao.countcomments();
    }

    @Override
    public Integer getCommentCountByBlogId(Long blogId) {
        return commentDao.getCommentCountByBlogId(blogId);
    }

    @Override
    public List<Comment> listCommentByBlogId (Long blogId){

        // 获取父级评论信息
        List<Comment> comments = commentDao.findByBlogIdParentIdNull(blogId);

        for(Comment comment : comments){
            Long commentid = comment.getCommentid();
            //根据父级评论id  获取所属子集评论信息
            List<Comment> childComments = commentDao.findParentIdNotNull(commentid);
            //查询出子评论
            combineChildren(childComments);
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }

        return comments;
    }


    @Transactional
    @Override
    public Integer saveComments(Comment comment) {
        try{
            // 头像为空时，默认一个头像
            if(comment.getAvatar()==null || comment.getAvatar()==""){
                comment.setAvatar(avatar);
            }
            // 判断是否存在父级评论
            if(comment.getParentcommentid() != -1){
                Comment comment1 = commentDao.getParentCommentById(comment.getParentcommentid());
                comment.setParentnickname(comment1.getNickname());
                comment.setParentComment(comment1);
            }else {
                comment.setParentcommentid(null);
            }
//            comment.setUpcomment(1);
//            comment.setAdmincomment(1);
            comment.setStatus(1);
            comment.setFlag(1);
            comment.setCreatetime(new Date());

            String ipApi = ipApiUrl+comment.getCommentlocation();
            if("0:0:0:0:0:0:0:1".equals(comment.getCommentlocation())){
                ipApi = ipApiUrl;
            }
            JSONObject jsonObject = JsonUtil.loadJson(ipApi);

            Boolean success = ((Boolean)jsonObject.get("success")).booleanValue();
            if(success){
                if("局域网".equals(String.valueOf(JSON.parseObject(jsonObject.get("info").toString()).get("country")))){
                    comment.setSourcecity("火星");
                }else{
                    comment.setSourcecity(String.valueOf(JSON.parseObject(jsonObject.get("info").toString()).get("prov")));
                }
            }else{
                comment.setSourcecity("火星");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return commentDao.saveComments(comment);
    }


    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    /**
     * @Description: 查询出子评论
     */
    private void combineChildren(List<Comment> childComments) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                tempReplys.add(childComment);
                Long childId = childComment.getCommentid();
                //查询二级以及所有子集回复
                recursively(childId);
            }
        }
    }

    /**
     * @Description: 循环迭代找出子集回复
     */
    private void recursively(Long childId) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentDao.findParentIdNotNull(childId);
        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                Long replayId = replayComment.getCommentid();
                tempReplys.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId);
            }
        }
    }



}
